/**
 * 
 * 
new MaxLazyLoad({
					wrapper:'content',
					seletor:'img',
					success:function(dom, src, parm){
						// console.log(dom, src, parm)
						dom.style.width = parm.width + 'px'
						dom.style.height = parm.height + 'px'
						console.log(a)
					},
					error:function(dom, src, parm){
						// console.log('err',dom, src, parm)
					}
				});
 */


;(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
	typeof define === 'function' && define.amd ? define(factory) :
	(global.MaxLazyLoad = factory());
}(this, (function () {
	if(this !== window){
		return;
	}
	var doc = document,win = this;
	var MaxLazyLoad = function(ops){
		this.options = {};
		this.initOPtions(ops);
		this.getNodeList();
		this.scroll();
		return this;
	};
	
	var PREVIMG = ''
	var DEFAULT_IMG = ''
	MaxLazyLoad.CONFIG = {
		doc: doc,
		wrapper: doc,										//wrapper为包裹容器，默认为document
		seletor: 'img',										//包裹容器内的seletor：选择器只能为单独的img标签或单个类样式选择器
		defaultImg: DEFAULT_IMG,					//图片加载出错的默认图片
		prevImg: PREVIMG,
		diff: 50,											//与底部的阈值
		success: '',										//图片加载成功的回调函数
		error: ''											//图片加载失败的回调函数
	};
	
	//初始化options
	MaxLazyLoad.prototype.initOPtions = function(ops) {
		var wrapper = (ops && ops.wrapper) || MaxLazyLoad.CONFIG.wrapper;
		this.options.wrapper = typeof wrapper === 'string' ? doc.getElementById(wrapper.indexOf("#") > -1 ? wrapper.substring(1) : wrapper) : wrapper;
		this.options.seletor = (ops && ops.seletor) || MaxLazyLoad.CONFIG.seletor;
		this.options.defaultImg = (ops && ops.defaultImg) || MaxLazyLoad.CONFIG.defaultImg;
		this.options.prevImg = (ops && ops.prevImg) || MaxLazyLoad.CONFIG.prevImg;
		this.options.diff = (ops && ops.diff) || MaxLazyLoad.CONFIG.diff;
		this.options.success = (ops && ops.success) || MaxLazyLoad.CONFIG.success;
		this.options.error = (ops && ops.error) || MaxLazyLoad.CONFIG.error;
		this.options.winH = win.innerHeight;
	};
	
	//获取操作的dom节点
	MaxLazyLoad.prototype.getNodeList = function(flag) {
		var seletor = this.options.seletor;
		if(seletor.indexOf('.') > -1){
			this.nodeList = this.options.wrapper.getElementsByClassName(seletor.substring(1));
		}else{
			this.nodeList = this.options.wrapper.getElementsByTagName(seletor);
		}
		this.ImgList(flag);
	}
	
	//得到操作节点的相关数据，保存至一个数组中
	MaxLazyLoad.prototype.ImgList = function(flag) {
		var _this = this;
		this.imgArr = []
		var nodeList = this.nodeList;
		for (var i = 0; i < nodeList.length; i++) {
			this.imgArr.push({
				img: nodeList[i],
				src:nodeList[i].getAttribute('data-src') || nodeList[i].src,
				offsetTop: nodeList[i].offsetTop
			})
			if(!nodeList[i].src){
				nodeList[i].src = _this.options.prevImg
			}
		}
		if(!flag){
			this.initLoadImg()
		}
	}
	
	//加载图片
	MaxLazyLoad.prototype.initLoadImg = function(scrollTop) {
		var _this = this;
		var winH = this.options.winH;
		var diff = this.options.diff;
		var top = scrollTop || 0;
		var imgArr = this.imgArr;
		var imgArrTemp = imgArr.slice();
		imgArr.forEach(function(item, index){
			if(item.offsetTop < (winH + diff + top)){
				_this.loadImg(item.img,item.src);
				imgArrTemp.shift(item);
			}
		})
		this.imgArr = imgArrTemp;
	}
	
	MaxLazyLoad.prototype.loadImg = function(dom,src) {
		var _this = this;
		var img = new Image();
		if(typeof document.onload == "undefined"){
			img.onreadystatechange = function(){
				if(this.readyState == "complete"){
	 				_this.successImg(dom, src, {
	 					width: img.width,
	 					height: img.height
	 				});
	 			}else{
					_this.errorImg(dom,src, {});
	 			}
			}
		}else{
			img.onload = function(){
				_this.successImg(dom, src, {
					width: img.naturalWidth ? img.naturalWidth : img.width,
					height: img.naturalWidth ? img.naturalHeight : img.height
				});
			}
			img.onerror = function(){
				_this.errorImg(dom, src, {});
			}
		}
		img.src = src;
	}
	
	//图片加载成功
	MaxLazyLoad.prototype.successImg = function(dom, src, parm) {
		dom.src = src;
		this.options.success && this.options.success(dom, src, parm);
	}
	
	//图片加载失败
	MaxLazyLoad.prototype.errorImg = function(dom, src, parm) {
		dom.src = this.options.defaultImg;
		this.options.error && this.options.error(dom, src, parm);
	}
	
	//滚动时间
	MaxLazyLoad.prototype.scroll = function() {
		var _this = this;
		win.onscroll = function(e){
			if(_this.imgArr && _this.imgArr.length){
				_this.initLoadImg(doc.body.scrollTop);
			}
		}
	}
	
	//刷新，重新获取为操作的dom相关数据
	MaxLazyLoad.prototype.refresh = function(){
		var index = this.imgArr.length;
		if(index > 0){
			this.getNodeList(true);
			var len = this.imgArr.length;
			this.imgArr = this.imgArr.splice(len-index);
		}
	}
	
	MaxLazyLoad.prototype.msg = function(msg){
		console.log("[MaxLazyLoad ERROR]" + msg);
	}
	
	return MaxLazyLoad;
	/*return win.MaxLazyLoad = function(ops){
		return new MaxLazyLoad(ops)
	}*/
})));


/*;(function(win,doc){
	var MaxLazyLoad = function(ops){
		this.options = {};
		this.initOPtions(ops);
		this.getNodeList();
		this.scroll();
		return this;
	};
	
	MaxLazyLoad.CONFIG = {
		doc: doc,
		wrapper: doc,										//wrapper为包裹容器，默认为document
		seletor: 'img',										//包裹容器内的seletor：选择器只能为单独的img标签或单个类样式选择器
		defaultImg: '../../img/pic.jpg',					//图片加载出错的默认图片
		prevImg: '../../maxLightBox/img/icon/loading.gif',	//图片默认的图片
		diff: 50,											//与底部的阈值
		success: '',										//图片加载成功的回调函数
		error: ''											//图片加载失败的回调函数
	};
	
	//初始化options
	MaxLazyLoad.prototype.initOPtions = function(ops) {
		var wrapper = (ops && ops.wrapper) || MaxLazyLoad.CONFIG.wrapper;
		this.options.wrapper = typeof wrapper === 'string' ? doc.getElementById(wrapper.indexOf("#") > -1 ? wrapper.substring(1) : wrapper) : wrapper;
		this.options.seletor = (ops && ops.seletor) || MaxLazyLoad.CONFIG.seletor;
		this.options.defaultImg = (ops && ops.defaultImg) || MaxLazyLoad.CONFIG.defaultImg;
		this.options.prevImg = (ops && ops.prevImg) || MaxLazyLoad.CONFIG.prevImg;
		this.options.diff = (ops && ops.diff) || MaxLazyLoad.CONFIG.diff;
		this.options.success = (ops && ops.success) || MaxLazyLoad.CONFIG.success;
		this.options.error = (ops && ops.error) || MaxLazyLoad.CONFIG.error;
		this.options.winH = win.innerHeight;
	};
	
	//获取操作的dom节点
	MaxLazyLoad.prototype.getNodeList = function(flag) {
		var seletor = this.options.seletor;
		if(seletor.indexOf('.') > -1){
			this.nodeList = this.options.wrapper.getElementsByClassName(seletor.substring(1));
		}else{
			this.nodeList = this.options.wrapper.getElementsByTagName(seletor);
		}
		this.ImgList(flag);
	}
	
	//得到操作节点的相关数据，保存至一个数组中
	MaxLazyLoad.prototype.ImgList = function(flag) {
		var _this = this;
		this.imgArr = []
		var nodeList = this.nodeList;
		for (var i = 0; i < nodeList.length; i++) {
			this.imgArr.push({
				img: nodeList[i],
				src:nodeList[i].getAttribute('data-src') || nodeList[i].src,
				offsetTop: nodeList[i].offsetTop
			})
			if(!nodeList[i].src){
				nodeList[i].src = _this.options.prevImg
			}
		}
		if(!flag){
			this.initLoadImg()
		}
	}
	
	//加载图片
	MaxLazyLoad.prototype.initLoadImg = function(scrollTop) {
		var _this = this;
		var winH = this.options.winH;
		var diff = this.options.diff;
		var top = scrollTop || 0;
		var imgArr = this.imgArr;
		var imgArrTemp = imgArr.slice();
		imgArr.forEach(function(item, index){
			if(item.offsetTop < (winH + diff + top)){
				_this.loadImg(item.img,item.src);
				imgArrTemp.shift(item);
			}
		})
		this.imgArr = imgArrTemp;
	}
	
	MaxLazyLoad.prototype.loadImg = function(dom,src) {
		var _this = this;
		var img = new Image();
		if(typeof document.onload == "undefined"){
			img.onreadystatechange = function(){
				if(this.readyState == "complete"){
	 				_this.successImg(dom, src, {
	 					width: img.width,
	 					height: img.height
	 				});
	 			}else{
					_this.errorImg(dom,src, {});
	 			}
			}
		}else{
			img.onload = function(){
				_this.successImg(dom, src, {
					width: img.naturalWidth ? img.naturalWidth : img.width,
					height: img.naturalWidth ? img.naturalHeight : img.height
				});
			}
			img.onerror = function(){
				_this.errorImg(dom, src, {});
			}
		}
		img.src = src;
	}
	
	//图片加载成功
	MaxLazyLoad.prototype.successImg = function(dom, src, parm) {
		dom.src = src;
		this.options.success && this.options.success(dom, src, parm);
	}
	
	//图片加载失败
	MaxLazyLoad.prototype.errorImg = function(dom, src, parm) {
		dom.src = this.options.defaultImg;
		this.options.error && this.options.error(dom, src, parm);
	}
	
	//滚动时间
	MaxLazyLoad.prototype.scroll = function() {
		var _this = this;
		win.onscroll = function(e){
			if(_this.imgArr && _this.imgArr.length){
				_this.initLoadImg(doc.body.scrollTop);
			}
		}
	}
	
	//刷新，重新获取为操作的dom相关数据
	MaxLazyLoad.prototype.refresh = function(){
		var index = this.imgArr.length;
		if(index > 0){
			this.getNodeList(true);
			var len = this.imgArr.length;
			this.imgArr = this.imgArr.splice(len-index);
		}
	}
	
	MaxLazyLoad.prototype.msg = function(msg){
		console.log("[MaxLazyLoad ERROR]" + msg);
	}
	
	return win.MaxLazyLoad = function(ops){
		return new MaxLazyLoad(ops)
	}
})(window,document);*/